package cn.kgm.makeGodV2.relationship;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-10-13 14:12
 * @Description: cn.kgm.makeGodV2.relationship.EmotionSystem
 */

import cn.kgm.makeGodV2.emotion.PersonalityTrait;

import java.util.HashMap;
import java.util.Map;

/**
 * 情感系统管理器
 */
public class EmotionSystem {
    private final Map<String, EmotionManager> emotionManagers;

    public EmotionSystem() {
        this.emotionManagers = new HashMap<>();
    }

    /**
     * 注册角色
     */
    public void registerCharacter(String characterId, PersonalityTrait personality) {
        emotionManagers.put(characterId, new EmotionManager(characterId, personality));
        System.out.printf("注册角色: %s - %s%n", characterId, personality);
    }

    /**
     * 获取情感管理器
     */
    public EmotionManager getEmotionManager(String characterId) {
        return emotionManagers.get(characterId);
    }

    /**
     * 处理情感事件
     */
    public void processEmotionEvent(String sourceId, String targetId, int delta, String reason) {
        EmotionManager sourceManager = emotionManagers.get(sourceId);
        if (sourceManager != null) {
            sourceManager.changeRelationship(targetId, delta, reason);
        }
    }

    /**
     * 处理双向情感事件
     */
    public void processBidirectionalEvent(String charA, String charB, int deltaA, int deltaB, String reason) {
        processEmotionEvent(charA, charB, deltaA, reason);
        processEmotionEvent(charB, charA, deltaB, reason);
    }

    /**
     * 获取双向关系视图
     */
    public void displayBidirectionalRelationship(String charA, String charB) {
        EmotionManager managerA = emotionManagers.get(charA);
        EmotionManager managerB = emotionManagers.get(charB);

        if (managerA == null || managerB == null) {
            System.out.println("角色不存在");
            return;
        }

        UnidirectionalRelationship relAtoB = managerA.getRelationship(charB);
        UnidirectionalRelationship relBtoA = managerB.getRelationship(charA);

        System.out.printf("=== %s 与 %s 的关系 ===%n", charA, charB);

        if (relAtoB != null) {
            System.out.println(relAtoB);
        } else {
            System.out.printf("%s -> %s: 无关系记录%n", charA, charB);
        }

        if (relBtoA != null) {
            System.out.println(relBtoA);
        } else {
            System.out.printf("%s -> %s: 无关系记录%n", charB, charA);
        }

        // 显示关系对称性
        if (relAtoB != null && relBtoA != null) {
            if (relAtoB.getType() == relBtoA.getType()) {
                System.out.printf("关系对称: 相互%s%n", relAtoB.getType().getDisplayName());
            } else {
                System.out.printf("关系不对称: %s视对方为%s, %s视对方为%s%n",
                        charA, relAtoB.getType().getDisplayName(),
                        charB, relBtoA.getType().getDisplayName());
            }
        }
        System.out.println();
    }

    /**
     * 显示角色所有关系
     */
    public void displayAllRelationships(String characterId) {
        EmotionManager manager = emotionManagers.get(characterId);
        if (manager == null) {
            System.out.println("角色不存在: " + characterId);
            return;
        }

        System.out.printf("=== %s 的所有关系 ===%n", characterId);
        Map<String, UnidirectionalRelationship> relationships = manager.getAllRelationships();

        if (relationships.isEmpty()) {
            System.out.println("  暂无关系记录");
        } else {
            for (UnidirectionalRelationship rel : relationships.values()) {
                System.out.println("  " + rel);
            }
        }
        System.out.println();
    }
}
